package com.kitty.game.activity.service;

import cn.hutool.core.date.DateUtil;
import com.kitty.common.model.Pos;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.drop.newservice.NewDropService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.StringUtil;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class YuLuXianChi {

    public static Date beginTime = null;
    public static Date endTime = null;
    private static boolean msgLogic = false;

    /**
     * 在本地图内玩家列表
     */

    public static String getTime() {
        return DateUtil.format(beginTime, "HH:mm") + "-" + DateUtil.format(endTime, "HH:mm");
    }
    /**
     * 菜单处理
     */
    public static void joinXianChiMap(Role role) {
        if (!checkIsStart()) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("仙池开放时间为" + getTime() + "，请耐心等候。"));
            return;
        }
        SpringUtils.getMapService().changeMap(role, 37013, 43, 26);


    }

    public static boolean checkIsStart() {
        try {
            Date nowTime = null;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            String ylxcOpenTime = DataCache.REDIS_DATA.get("ylxc_open_time");

            if (StringUtil.isEmpty(ylxcOpenTime)) {
                ylxcOpenTime = "19:30-20:00";
            }
            String[] split = ylxcOpenTime.split("-");
            try {
                nowTime = df.parse(df.format(new Date()));
                beginTime = df.parse(split[0]);
                endTime = df.parse(split[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);

            Calendar begin = Calendar.getInstance();
            begin.setTime(beginTime);

            Calendar end = Calendar.getInstance();
            end.setTime(endTime);

            if (date.after(begin) && date.before(end)) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }

        return false;
    }
    @Scheduled(cron = "0/10 * * * * ?")
    public void sendReward() {
        if (checkIsStart()) {
            for (Map.Entry<Long, Role> longRoleEntry : DataCache.ONLINE_ROLES.entrySet()) {
                Role value = longRoleEntry.getValue();
                Pos pos = value.getPos();
                if(pos.getMapId() == 37013){
                    String ylxcReward = DataCache.REDIS_DATA.get("ylxc_reward");
                    //发送奖励
                    for (String s : ylxcReward.split("\\+")) {
                        SpringUtils.getBean(NewDropService.class).addRewardTask(longRoleEntry.getValue(), s, "");
                    }
                }
            }
        }
        sendStartMsg();
    }

    public static void sendStartMsg() {
        if (checkIsStart()) {
            if (!msgLogic){
                SpringUtils.getChatService().sendAdnotice( "玉露仙池泡澡活动已开启！请各位道友速速前往天墉城仙池泡澡处参与！！！");
                msgLogic = true;
            }
        } else {
            msgLogic = false;
        }
    }
}
